$(document).ready(function (){
		sessionUrl();
	showsession();
	initpost();
	//点击提交
/* 	$('#uploadImg').click(function(){
		showMistAndWait();
		var fileName = $("#uploadImg").val();
		if(fileName==""){
			//点击取消，取消事件的编写
			hideMistAndWait();
			return;
		}
	}); */
	
	
	$('.postBtn').click(function(){
		var title 	= $('#askTitle').val();					//问题标题
		var content = $('.questionContent').html();			//问题内容，全部DIV
		//校验是否有标题和内容，如果没有就报错
		if(title == ''){
			alert('请填写标题');
			$('#askTitle').focus();
		}else if(content ==''){
			alert('请填写问题描述');
			$('#questionContent').focus();
		}else{
			dealpicpath();  //修改后台路径
			dealpicsrc();   //修改连接src
			var content = $('.questionContent').html();		//重新拿内容
			 $.ajax({
				type:'post',
				url:'../actions/c2cAction.php',
				data:{
					act			:'newpost',
					title		:title,
					content		:content
				}, 
				async: true,	
				beforeSend: function(){
					showMistAndWait();
				},
				success:function(data){
					hideMistAndWait();
					console.log(data);
					alert('发布成功！');
					window.location.href = "street.php#postid"+ data ;
				}
			});     
		}
	});
});

//图片上传函数
function fileupload(file){
	var files  = $('#uploadImg').prop('files');
	var imgid  = new Date().getTime(); //获取当前时间戳
	var reader = new FileReader();
	reader.readAsDataURL(files[0]);
	reader.onload = function(e){
	var imgsrc = e.target.result;
	//console.log('imgsrc:' + imgsrc);
	showMistAndWait();
		$.ajax({
			type:'post',
			url:'../actions/c2cAction.php',
			data:{
				act			:'newpic',
				imgsrc		:imgsrc,
				imgid		:imgid
			}, 
			async: true,	
			beforeSend: function(){
				showMistAndWait();
			},
			success:function(data){
				console.log(data);
				previewB(imgid,data);
				alert('图片上传成功！');
				hideMistAndWait();
			}
		});    
	}
}

//预览函数B
function previewB(imgid,imgsrc){
	var img = "<div class=''><img src='" + imgsrc + "' id = '" + imgid + "' lastsrc='" + imgsrc +  "'/></div><br/>";
	$('.questionContent').append(img);
}

//设置进度条
function setprogress(percent){
	$('.progress').removeClass('hide');
	$('.progress-bar').css('width',percent+'%');
	$('.progress-bar').text(percent+'%');
}

//初始化函数，将userid下的tmp文件夹下的文件情况
function initpost(){
	$.ajax({
		type:'post',
		url:'../actions/c2cAction.php',
		data:{
			act	:'initpost',
		}, 
		async: true,	
		beforeSend: function(){
			//showMistAndWait();
		},
		success:function(data){
			//hideMistAndWait();
			console.log(data);
			//alert(data);
		}
	});   
}

//处理div中的img的src路径，将其从base64变更为userid下的pic的url
function dealpicsrc(){
	var content = $('.questionContent').html();
	var imgs 	= $('.questionContent').find('img');
	for(i=0;i<imgs.length;i++){
		var imgId 	= imgs[i].id;
		var lastsrc = $('#'+imgId).attr('lastsrc');
		lastsrc = lastsrc.replace('/tmp','');
		var ii = document.getElementById(imgId);
		ii.src = lastsrc;
	}
}

//将tmp下的文件移动下userid的文件夹下。
function dealpicpath(){
	$.ajax({
		type:'post',
		url:'../actions/c2cAction.php',
		data:{
			act	:'dealpicpath',
		}, 
		async: true,	
		beforeSend: function(){
			//showMistAndWait();
		},
		success:function(data){
			//hideMistAndWait();
			console.log(data);
			//alert(data);
		}
	});   
}
